package org.bolmart.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.flex.remoting.RemotingInclude;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

@Service("bolmartService")
@RemotingDestination(channels = { "my-amf" })
public class BolmartService {
	protected HibernateTemplate template;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		template = new HibernateTemplate(sessionFactory);
	}

/******************* Customer Block******************/

	@RemotingInclude
	public List<Customer> getCustomers() {
		return template.findByExample(new Customer());
	}
	
	@RemotingInclude
	public void addCustomer(String firstName, String lastName, String phone, String email) {
		Customer customer = new Customer();
		customer.setFirstName(firstName);
		customer.setLastName(lastName);
		customer.setPhone(phone);
		customer.setEmail(email);
		template.save(customer);
	}
	
	@RemotingInclude
	public void delCustomer(int idCustomer) {
		Customer customer = template.get(Customer.class, idCustomer);
		template.delete(customer);
	}
	
	@RemotingInclude
	public void updCustomer(int idCustomer, String firstName, String lastName) {
		Customer customer = template.get(Customer.class, idCustomer);
		customer.setFirstName(firstName);
		customer.setLastName(lastName);
		template.saveOrUpdate(customer);
	}
/******************* end Customer Block******************/

/******************* Custorder Block******************/

//	@RemotingInclude
//	public List<Custorder> getCustorders() {
//		return template.findByExample(new Custorder());
//	}
	
	@RemotingInclude
	public List<Custorder> getCustorders(int idCustomer) {
		Customer customer = template.get(Customer.class, idCustomer);
		return template.find("from org.bolmart.dao.Custorder custorder where custorder.customer=?", customer);
	}
	
	@RemotingInclude
	public void addCustorder(double price, double paid, String custorderStatus,
			Date openDate, Date closeDate, Customer customer, Shoporder shoporder) {
		Custorder custorder = new Custorder();
		
		custorder.setPrice(price);
		custorder.setPaid(paid);
		custorder.setCustorderStatus(custorderStatus);
		custorder.setOpenDate(openDate);
		custorder.setCloseDate(closeDate);
		custorder.setCustomer(customer);
		List<Item> items = new ArrayList<Item>(); 
		custorder.setItems(items);
		custorder.setShoporder(shoporder);
		
		template.save(custorder);
	}
	
	@RemotingInclude
	public void delCustorder(int idCustorder) {
		Custorder custorder = template.get(Custorder.class, idCustorder);
		template.delete(custorder);
	}
	
	@RemotingInclude
	public void updCustorder(int idCustorder, double price, double paid, String custorderStatus,
			Date openDate, Date closeDate, Customer customer, Shoporder shoporder) {
		Custorder custorder = template.get(Custorder.class, idCustorder);
		
		custorder.setPrice(price);
		custorder.setPaid(paid);
		custorder.setCustorderStatus(custorderStatus);
		custorder.setOpenDate(openDate);
		custorder.setCloseDate(closeDate);
		custorder.setCustomer(customer);
		List<Item> items = new ArrayList<Item>(); 
		custorder.setItems(items);
		custorder.setShoporder(shoporder);
		
		template.saveOrUpdate(customer);
	}

/******************* End Custorder Block******************/

/******************* Shoporder Block******************/
	
	@RemotingInclude
	public List<Shoporder> getShoporders() {
		return template.findByExample(new Shoporder());
	}
	
//	@RemotingInclude
//	public List<Shoporder> getShoporders(int idShoporder) {
//		Shoporder shoporder = template.get(Shoporder.class, idShoporder);
//		return template.find("from org.bolmart.dao.Shoporder shoporder where shoporder.customer=?", shoporder);
//	}
	
	@RemotingInclude
	public void addShoporder(String carrier, String tracking, String shoporderStatus,
			Date sendDate, Date deliveryDate) {
		Shoporder shoporder = new Shoporder();
		
		shoporder.setCarrier(carrier);
		shoporder.setTracking(tracking);
		shoporder.setShoporderStatus(shoporderStatus);
		shoporder.setSendDate(sendDate);
		shoporder.setDeliveryDate(deliveryDate);
		List<Custorder> custorders = new ArrayList<Custorder>();
		shoporder.setCustorders(custorders);
		
		template.save(shoporder);
	}
	
	@RemotingInclude
	public void delShoporder(int idShoporder) {
		Shoporder shoporder = template.get(Shoporder.class, idShoporder);
		template.delete(shoporder);
	}
	
	@RemotingInclude
	public void updShoporder(int idShoporder, String carrier, String tracking, String shoporderStatus,
			Date sendDate, Date deliveryDate) {
		Shoporder shoporder = template.get(Shoporder.class, idShoporder);
		
		shoporder.setCarrier(carrier);
		shoporder.setTracking(tracking);
		shoporder.setShoporderStatus(shoporderStatus);
		shoporder.setSendDate(sendDate);
		shoporder.setDeliveryDate(deliveryDate);
		List<Custorder> custorders = new ArrayList<Custorder>();
		shoporder.setCustorders(custorders);

		template.saveOrUpdate(shoporder);
	}
/******************* End Shoporder Block******************/	
	
/******************* Item Block******************/	
	
	@RemotingInclude
	public List<Shoporder> getItems(int idCustorder) {
		Custorder custorder = template.get(Custorder.class, idCustorder);
		return template.find("from org.bolmart.dao.Item item where item.custorder=?", custorder);
	}
	
	@RemotingInclude
	public void addItem(String itemName, String shopName, String itemStatus,
			String shopLink, Custorder custorder) {
		Item item = new Item();
		
		item.setItemName(itemName);
		item.setShopName(shopName);
		item.setItemStatus(itemStatus);
		item.setShopLink(shopLink);
		item.setCustorder(custorder);
		
		template.save(item);
	}
	
	@RemotingInclude
	public void delItem(int idItem) {
		Item item = template.get(Item.class, idItem);
		template.delete(item);
	}
	
	@RemotingInclude
	public void updItem(int idItem, String itemName, String shopName, String itemStatus,
			String shopLink, Custorder custorder) {
		Item item = template.get(Item.class, idItem);
		
		item.setItemName(itemName);
		item.setShopName(shopName);
		item.setItemStatus(itemStatus);
		item.setShopLink(shopLink);
		item.setCustorder(custorder);

		template.saveOrUpdate(item);
	}	
/******************* end Item Block******************/


}
